package com.bobi.tfca.controller.message;

import com.bobi.tfca.model.entity.MemberEntity;
import com.bobi.tfca.model.pojo.R;
import com.bobi.tfca.model.vo.ChatRoom;
import com.bobi.tfca.model.vo.MemberVO;
import com.bobi.tfca.service.GroupMemberService;
import com.bobi.tfca.service.GroupService;
import com.bobi.tfca.web.annotation.CurrentUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 聊天室接口
 *
 * @author 李文发
 * @date 2024/4/8
 */
@RestController
@RequestMapping("/chatRoom")
@Api(tags = "聊天室接口")
@Slf4j
public class ChatRoomController {

    @Autowired
    private GroupService groupService;


    @GetMapping("/findAll")
    @ApiOperation(value = "查询全部聊天室")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "member", value = "用户信息")
    })
    public R<List<ChatRoom>> findAll(@CurrentUser MemberEntity member) {
        log.info(member.toString());
        List<ChatRoom> res = groupService.findAllChatRooms(member.getMemberId());
        return R.data(res);
    }

    @PostMapping("/editGroupName/{groupId}")
    @ApiOperation(value = "修改群名")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "群聊id"),
            @ApiImplicitParam(name = "groupName", value = "群聊名称")
    })
    public R<String> editGroupName(@PathVariable("groupId") Integer groupId, @RequestBody String groupName) {
        groupService.editGroupName(groupId, groupName);
        return R.success("修改群名成功！");
    }

    @GetMapping("/disbandGroup/{groupId}")
    @ApiOperation(value = "解散群聊")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "群聊id")
    })
    public R<String> disbandGroup(@PathVariable("groupId") Integer groupId) {
        groupService.disbandGroup(groupId);
        return R.success("解散群聊！");
    }

    @PostMapping("/addMember2Group/{groupId}")
    @ApiOperation(value = "添加成员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "群聊id"),
            @ApiImplicitParam(name = "choseMemberIdList", value = "成员Id")
    })
    public R<String> addMember2Group(@PathVariable("groupId") Integer groupId, @RequestBody List<Integer> choseMemberIdList) {
        groupService.addMember2Group(groupId, choseMemberIdList);
        return R.success("添加成员成功！");
    }

    @PostMapping("/removeMembers")
    @ApiOperation(value = "移除成员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "member", value = "当前用户"),
            @ApiImplicitParam(name = "memberIds", value = "移除用户列表"),
            @ApiImplicitParam(name = "groupId", value = "群聊id")
    })
    public R<String> removeMembers(@CurrentUser MemberEntity member, @RequestParam(value = "memberIds") List<Integer> memberIds, @RequestParam(value = "groupId") Integer groupId) {
        Integer memberId = member.getMemberId();
        if (memberIds.contains(memberId)) {
            return R.error("不能移除自己");
        }
        // TODO 角色权限校验
        groupService.removeMembers(memberIds, groupId);
        return R.success("移除成员成功！");
    }

    @GetMapping("/findAllGroupMembers/{groupId}")
    @ApiOperation(value = "查询全部群成员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "群聊id")
    })
    public R<List<MemberVO>> findAllGroupMembers(@PathVariable("groupId") Integer groupId) {
        List<MemberVO> memberVOList = groupService.findAllGroupMembers(groupId);
        return R.data(memberVOList);
    }

    @GetMapping("/getGroupMemberHeadList/{groupId}")
    @ApiOperation(value = "查询详情页面会员头像粗略展示")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "群聊id")
    })
    public R<List<MemberVO>> getGroupMemberHeadList(@PathVariable("groupId") Integer groupId) {
        List<MemberVO> memberVOList = groupService.getGroupMemberHeadList(groupId);
        return R.data(memberVOList);
    }

    @PostMapping("/createGroup")
    @ApiOperation(value = "创建群聊")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupName", value = "群聊名称"),
            @ApiImplicitParam(name = "choseMemberIdList", value = "成员Id")
    })
    public R<List<MemberVO>> createGroup(@CurrentUser MemberEntity member, String groupName,  @RequestParam(value = "choseMemberIdList", required = false) List<Integer> choseMemberIdList) {
        Integer groupOwnerId = member.getMemberId();
        groupService.createGroup(groupOwnerId, groupName, choseMemberIdList);
        return R.success("创建群聊成功");
    }

    @ApiOperation(value = "修改群头像")
    @PostMapping("/changeGroupHead/{groupId}")
    @ApiImplicitParams({@ApiImplicitParam(name = "groupId", value = "群id"),
            @ApiImplicitParam(name = "file", value = "图片")})
    public R<String> changeGroupHead(@PathVariable("groupId") Integer groupId, @RequestPart("file") MultipartFile file) {
        String url = groupService.changeGroupHead(groupId, file);
        return R.data(url);
    }
}
